//
//  TPStationDetailController.m
//  TravelProject
//
//  Created by lanou3g on 15/11/26.
//  Copyright © 2015年 韩婷婷. All rights reserved.
//

#import "TPStationDetailController.h"
#import "TPStationCell.h"
#import "MJRefresh.h"
#import "HT_DataDownloadTools.h"
#import "RecomendSubjectModel.h"
#import "UIImageView+WebCache.h"


@interface TPStationDetailController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collection;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong) NSMutableArray *picArray;
@property (nonatomic,assign) NSInteger page;

@end

@implementation TPStationDetailController

+(instancetype)shareStationVC{
    static TPStationDetailController *station = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        station = [mainStoryBoard instantiateViewControllerWithIdentifier:@"station"];
    });
    return station;
}
//懒加载
- (NSMutableArray *)picArray{
    if (!_picArray) {
        _picArray = [NSMutableArray array];
    }
    return _picArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.navigationItem.title = @"专题列表";
    [self setupCollectionView];
    //集成刷新控件
    [self addHeader];
    [self addFooter];
    
    
    
    // Do any additional setup after loading the view.
}
- (void)addHeader{
    __unsafe_unretained typeof (self) vc = self;
    [self.collection addHeaderWithCallback:^{
        self.page = 1;
        NSString *stringURL = [NSString stringWithFormat:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=865308021990677&track_app_version=6.8.2&track_app_channel=hicloud&track_device_info=hwH30-T00&track_os=Android4.2.2&app_installtime=1448007324702&lat=40.030244&lon=116.343632&page=%ld&count=10",self.page];

        [HT_DataDownloadTools downloadDataWithURL:stringURL andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSArray *array = dic[@"data"];
            for (NSDictionary *d in array) {
                RecomendSubjectModel *model = [[RecomendSubjectModel alloc]init];
                [model setValuesForKeysWithDictionary:d];
                [self.picArray addObject:model];
            }
            [vc.collection reloadData];
            [vc.collection headerEndRefreshing];
        }];
    }];
     [self.collection headerBeginRefreshing];
}
- (void)addFooter{
    __unsafe_unretained typeof(self) vc = self;
    [self.collection addFooterWithCallback:^{
        if (self.page < 5) {
            self.page ++;
              NSString *stringURL = [NSString stringWithFormat:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=865308021990677&track_app_version=6.8.2&track_app_channel=hicloud&track_device_info=hwH30-T00&track_os=Android4.2.2&app_installtime=1448007324702&lat=40.030244&lon=116.343632&page=%ld&count=10",self.page];
            [HT_DataDownloadTools downloadDataWithURL:stringURL andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                NSArray *array = dic[@"data"];
                for (NSDictionary *d in array) {
                    RecomendSubjectModel *model = [[RecomendSubjectModel alloc]init];
                    [model setValuesForKeysWithDictionary:d];
                    [self.picArray addObject:model];
                }
                [vc.collection reloadData];
                [vc.collection footerEndRefreshing];
            }];
        }
    }];
}
- (void)setupCollectionView{
   _flowLayout.itemSize = CGSizeMake((CGRectGetWidth([UIScreen mainScreen].bounds)-30), 220);
    _flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collection.delegate = self;
    self.collection.dataSource =self;
    UINib *nib1 = [UINib nibWithNibName:@"TPStationCell" bundle:[NSBundle mainBundle]];
    [self.collection registerNib:nib1 forCellWithReuseIdentifier:@"stationCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.picArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TPStationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"stationCell" forIndexPath:indexPath];
    RecomendSubjectModel *model = self.picArray[indexPath.row];
    [cell.imagePicture sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
